<template>
	<view>
		<view class="" v-if="Number(checkCertification) == 2"
			style="width: 100%;text-align: center;color: red;background-color: #ffffff;line-height: 60rpx;">
			{{checkCertificationMessage}}
		</view>
		<view class="" v-if="checkCertification !='' && Number(checkCertification) === 0"
			style="width: 100%;text-align: center;color: green;background-color: #ffffff;line-height: 60rpx;">
			审核中
		</view>
		<view class="padding">
			<view class=" padding-lr bg-white " style="border-radius: 24rpx;">
				<u-form :model="form" ref="uForm" label-position="top" :label-style='lableStyle'>
					<u-form-item label="姓名" :border-bottom='true'>
						<u-input placeholder="请输入姓名" v-model="form.realName" />
					</u-form-item>
					<u-form-item label="出生日期" :border-bottom='true'>
						<view @click="openTime()">
							<view v-if="form.birthMention">{{form.birthMention}}</view>
							<view v-else style="color: #c0c4cc;">请选择出生日期（必填）</view>
							<!-- <u-input placeholder="请填写（必填）" v-model="form.birthMention" :disabled="true" /> -->
						</view>
					</u-form-item>
					<u-form-item label="驾龄（年）" :border-bottom='false'>
						<u-input placeholder="请填写驾龄（必填）" type="number" v-model="form.drivingYear" />
					</u-form-item>
				</u-form>
			</view>

			<view class=" padding bg-white margin-top " style="border-radius: 24rpx;">
				<view class="text-bold text-black">上传身份证件</view>
				<view class="flex align-center justify-between">
					<view class="flex margin-top-sm" style="overflow: hidden;flex-wrap: wrap;">
						<view class=" flex margin-right-sm " @click="addImages(2)">
							<view style="width: 293rpx;height: 181rpx;margin-right: 2rpx;position: relative;">
								<image :src="form.identityCardFront?form.identityCardFront:'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/two.png'"
									style="width: 100%;height: 100%;border-radius: 12rpx;" mode="aspectFill"></image>
							</view>
						</view>
					</view>
					<view class="flex margin-top-sm" style="overflow: hidden;flex-wrap: wrap;">
						<view style="width: 293rpx;height: 181rpx;margin-right: 2rpx;position: relative;"
							@click="addImages(3)">
							<image :src="form.identityCardBack?form.identityCardBack:'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/one.png'"
								style="width: 100%;height: 100%;border-radius: 12rpx;" mode="aspectFill"></image>
						</view>
					</view>
				</view>
			</view>
			<view class=" padding bg-white margin-top " style="border-radius: 24rpx;">
				<view class="text-bold text-black">上传驾驶证</view>
				<view class="flex align-center justify-between">
					<view class="flex margin-top-sm" style="overflow: hidden;flex-wrap: wrap;width: 100%;">
						<view style="width: 100%;" class=" flex margin-right-sm " @click="addImages(4)">
							<view style="width: 100%;height: 370rpx;margin-right: 2rpx;position: relative;">
								<image :src="form.drivingLicence?form.drivingLicence:'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/jsz.png'"
									style="width: 100%;height: 100%;border-radius: 12rpx;" mode="aspectFill"></image>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="Number(checkCertification) == 2 || checkCertification=='' || !checkCertification"
				class="padding bg-white radius margin-top">
				<view class="text-bold" style="color: #3699FF;">缴纳保证金</view>
				<view class="flex justify-between margin-top-sm">
					<view class="text-gray">满足订单量及好评率即可申请退还保证金</view>
					<view class="text-bold" style="color:#F1413D;">¥{{money}}</view>
				</view>
			</view>
			<view v-if="Number(checkCertification) == 2 || checkCertification=='' || !checkCertification"
				class=" margin-top footer text-xxl">
				<text @tap="isShowAgree" class="cuIcon" :class="showAgree?'cuIcon-radiobox':'cuIcon-round'">同意并接受</text>
				<!-- 协议地址 -->
				<navigator url="/my/setting/paotuixieyi" open-type="navigate">《师傅入驻协议》</navigator>
			</view>
			<view style="height: 120rpx;"></view>
		</view>
		<view v-if="Number(checkCertification) == 2 || checkCertification=='' || !checkCertification"
			class="bg-white flex justify-between align-center padding-lr"
			style="position: fixed;bottom: 0;width: 100%;height: 120rpx;">
			<view class="">
				<text class="text-lg text-bold text-black">支付：</text>
				<text style="color: #F1413D;">¥{{money}}</text>
			</view>
			<view>
				<u-button v-if="!disabled" @click="submit" class="" :custom-style="customStyle" shape="circle"
					:hair-line="false">认证并支付</u-button>
			</view>
		</view>

		<u-popup v-model="showpay" mode="bottom">
			<view class="popup_pay">
				<view class="bg margin-top padding-lr radius">
					<view style="padding: 0 20upx;margin-top: 36rpx;">
						<view
							style="display: flex;height: 100upx;align-items: center;padding: 20upx 0;justify-content: center;"
							v-for="(item,index) in openLists" :key='index'>
							<image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;">
							</image>
							<view style="font-size: 30upx;margin-left: 20upx;width: 70%;">
								{{item.name}}
							</view>
							<radio-group name="openWay" style="margin-left: 45rpx;" @tap='selectWay(item)'>
								<label class="tui-radio">
									<radio color="#02B595" :checked="openWay == item.id ? true : false" />
								</label>
							</radio-group>
						</view>
					</view>
				</view>
				<view class="pay_btns" @click="pay()">确认支付</view>
			</view>
		</u-popup>

		<u-picker v-model="showTime" mode="time" @confirm="confirmTime"></u-picker>
	</view>
</template>

<script>
	import configdata from '@/common/config.js';
	export default {
		data() {
			return {
				form: {
					realName: '',
					birthMention: '',
					drivingYear: '',
					// phone: '',
					// identityCardNumber: '',
					// identityCardFront: '',
					drivingLicence: '', //驾驶证
					// identityCardVerso: '',
					identityCardFront: '', //身份证正面
					identityCardBack: '', //身份证背面
					// cashDeposit: '',
					// DrivingAge: '',
					// shopImg: ''
				},
				lableStyle: {
					color: '#000',
					fontSize: '28upx',
					fontWeight: 800,
				},
				customStyle: {
					backgroundColor: '#02B595',
					color: '#FFFFFF',
					border: 0,
					width: '200rpx'
				},
				btnStyle: {
					backgroundColor: '#02B595',
					color: '#FFFFFF',
				},
				money: 0,
				showAgree: false, //协议是否选择
				sessionkey: '',
				xcxSelect: '否',
				showpay: false,
				openWay: 1,
				openLists: [],
				indentNumber: '',
				disabled: false,
				showTime: false,
				checkCertification: '',
				checkCertificationMessage: '',
			}
		},
		onLoad() {
			// this.xcxSelect = uni.getStorageSync('xcxSelect')
			// if (this.xcxSelect != '否') {
			// 	uni.setNavigationBarTitle({
			// 		title: '关于我们'
			// 	})
			// } else {
			// 	uni.setNavigationBarTitle({
			// 		title: '司机入驻'
			// 	})
			// }
			this.getUserInfo()
			// #ifdef H5
			let ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('micromessenger') !== -1) {
				//公众号是否自动登录  416
				this.$Request.get('/app/common/type/416').then(res => {
					if (res.data && res.data.value && res.data.value == '是') {
						this.openLists = [{
							id: 2,
							image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/zhifubao.png',
							name: '支付宝'
						}, {
							id: 1,
							image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/weixin.png',
							name: '微信'
						}];
						this.openWay = 1;
					} else {
						this.openLists = [{
							id: 2,
							image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/zhifubao.png',
							name: '支付宝'
						}];
						this.openWay = 2;
					}
				})
			} else {
				this.openLists = [{
					id: 2,
					image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/zhifubao.png',
					name: '支付宝'
				}];
				this.openWay = 2;
			}
			// #endif
			// #ifdef MP-WEIXIN
			this.openLists = [{
				id: 1,
				image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/weixin.png',
				name: '微信'
			}, ]
			this.openWay = 1;
			// #endif
			// #ifdef APP-PLUS
			this.openLists = [{
					id: 1,
					image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/weixin.png',
					name: '微信'
				},
				{
					id: 2,
					image: 'https://yuexiaoda-1314351529.cos.ap-shanghai.myqcloud.com/huoyunsf/zhifubao.png',
					name: '支付宝'
				}
			]
			this.openWay = 1;
			// #endif

			this.getRZmoney()
			this.sessionkey = this.$queue.getData("sessionkey");
		},
		methods: {
			getUserInfo() {
				this.$Request.getT('/app/userinfo/findUserInfoById').then(res => {
					if (res.code == 0) {
						this.form.realName = res.data.realName
						this.form.birthMention = res.data.birthMention
						this.form.drivingYear = res.data.drivingYear
						this.form.identityCardFront = res.data.identityCardFront
						this.form.drivingLicence = res.data.drivingLicence
						this.form.identityCardBack = res.data.identityCardBack
						//判断认证状态不等于null 不等于undefined
						if (res.data.checkCertification !== null && res.data.checkCertification !== void 0) {
							this.checkCertification = res.data.checkCertification
						}

						this.checkCertificationMessage = res.data.checkCertificationMessage
					} else {
						this.$queue.logout();
						uni.showModal({
							showCancel: false,
							title: '登录失败',
							content: res.msg,
						});
					}
				})
			},
			openTime() {
				console.log('========')
				this.showTime = true
			},
			confirmTime(e) {
				console.log(e);
				this.form.birthMention = e.year + '-' + e.month + '-' + e.day
			},
			isShowAgree() {
				this.showAgree = !this.showAgree
			},
			getRZmoney() {
				this.$Request.get("/app/common/type/261").then(res => {
					if (res.code == 0) {
						this.money = res.data.value
						this.form.cashDeposit = res.data.value
					}
				});
			},
			selectWay: function(item) {
				this.openWay = item.id;
			},
			submit() {
				let that = this
				if (!that.form.realName) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.birthMention) {
					uni.showToast({
						title: '请选择出生日期',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.drivingYear) {
					uni.showToast({
						title: '请填写驾龄',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.identityCardFront) {
					uni.showToast({
						title: '请上传身份证正面',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.identityCardBack) {
					uni.showToast({
						title: '请上传身份证背面',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.drivingLicence) {
					uni.showToast({
						title: '请上传驾驶证正面',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!this.showAgree) {
					uni.showToast({
						title: '请勾选司机服务用户协议',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				this.showpay = true
			},
			//上传身份证
			addImages(e) {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < 1; i++) {
							this.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://huoyunsj.xianmaxiong.com/sqx_fast/alioss/upload', //真实的接口地址
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									console.log(uploadFileRes)
									if (e == 2) { //身份证正面
										this.form.identityCardFront = JSON.parse(uploadFileRes
											.data).data
									} else if (e == 3) { //身份证发面
										this.form.identityCardBack = JSON.parse(uploadFileRes.data)
											.data
									} else if (e == 4) { //驾驶证
										this.form.drivingLicence = JSON.parse(uploadFileRes.data)
											.data
									}
									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
			pay() {
				if (this.openWay == 0) {
					this.$queue.showToast('请选择支付方式!')
					return;
				}
				if (this.openWay == 1) {
					console.log('微信')
					// #ifdef H5
					// 微信H5支付 根据订单id获取支付信息
					this.$Request.postJson("/app/wxPayRider/wxPayMpRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '微信支付信息')
						this.callPay(res.data);
					});
					// #endif
					// #ifdef APP-PLUS
					// 微信APP支付 根据订单id获取支付信息
					this.$Request.postJson("/app/wxPayRider/appRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '微信支付信息')
						this.isCheckPay(res.code, 'wxpay', JSON.stringify(res.data));
					});
					// #endif
					// #ifdef MP-WEIXIN
					let that = this
					that.$Request.postJson("/app/wxPayRider/wxPayJsApiRiderCertification", that.form).then(res => {
						if (res.code == 0) {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timestamp,
								nonceStr: res.data.noncestr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.sign,
								success: function(res) {
									console.log(res)
									uni.hideLoading();
									uni.showToast({
										title: '实名认证提交成功',
										icon: 'none'
									});
									setTimeout(function() {
										uni.navigateBack(1)
									}, 1000)
								},
								fail: function(err) {
									console.log('fail:' + JSON.stringify(err));
									uni.hideLoading();
									that.$queue.showToast('支付失败');
								}
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
					// #endif
				} else {
					// APP支付宝支付
					// #ifdef H5
					this.form.payType = 4
					this.$Request.postJson('/app/aliPay/aliPayRiderCertification', this.form).then(
						res => {
							if (res.code == 0) {
								const div = document.createElement('div')
								div.innerHTML = res.data //此处form就是后台返回接收到的数据
								document.body.appendChild(div)
								document.forms[0].submit()
							} else {
								uni.showToast({
									icon: 'none',
									title: '支付失败!'
								});
							}
						});
					// #endif

					// #ifdef APP
					this.form.payType = 5
					this.$Request.postJson("/app/aliPay/aliPayRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '支付宝支付信息')
						this.isCheckPay(res.code, 'alipay', res.data)
					});
					// #endif
				}
			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				// if (!response.package) {
				// 	return;
				// }
				console.log(response, 111111)
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						console.log(res, '/*-/*-/*-')
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.showLoading({
								title: '支付成功'
							});
							setTimeout(function() {
								uni.hideLoading();
								uni.navigateBack()
							}, 1000);
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			isCheckPay(status, name, order) {
				if (status == 0) {
					this.setPayment(name, order);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: '支付信息有误',
						icon: 'none'
					});
				}
			},
			setPayment(name, order) {
				console.log(name, '*-*-*', order)
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						console.log(res)
						uni.hideLoading();
						uni.showLoading({
							title: '支付成功'
						});
						setTimeout(function() {
							uni.navigateBack()
						}, 1000);
					},
					fail: function(err) {
						console.log(err)
						uni.hideLoading();
					},
					complete() {
						uni.hideLoading();
					}
				});
			},
		}
	}
</script>

<style lang="less">
	page {
		background: #F5F5F5;
	}

	.footer {
		/* // padding-left: 140upx; */
		/* justify-content: center; */
		margin-top: 32upx;
		font-size: 24upx;
		color: #666666;
		display: flex;
		// text-align: center;
		// align-items: center;
	}

	/* 支付弹框 */
	.popup_pay {
		width: 100%;
	}

	.pay_btns {
		width: 90%;
		margin: 0 auto 40rpx;
		text-align: center;
		background: #02B595;
		height: 80rpx;
		border-radius: 16rpx;
		color: #ffffff;
		line-height: 80rpx;
		margin-top: 20rpx;
	}
</style>